/**
 * @description [ mock 服务启动文件 ] 
 * @author Hilary
 * @date 2019/11/21
 */
import express from 'express'
import bodyParser from 'body-parser'
import morgan from 'morgan'
import cors from 'cors'
import http from 'http'
import path from 'path'
import yaml from 'yamljs'
import * as api from './api'

const app = express()
const port = 9956
const { connector, summarise } = require('swagger-routes-express')

// Logger 日志
app.use(morgan('dev...'))
// Enable CORS 跨域
app.use(cors())
// POST, PUT, DELETE body parser
app.use(bodyParser.json({ limit: '20mb' }))
app.use(bodyParser.urlencoded({
    limit: '20mb',
    extended: false
}))
// No cache
app.use((req, res, next) => {
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate')
    res.header('Pragma', 'no-cache')
    res.header('Expires', '-1')
    next()
})

// 使用静态文件
app.use("/static", express.static('public'))

// Read and swagger config file
const apiDefinition = yaml.load(path.resolve(__dirname, 'public/swagger.yaml'))
// // Create mock functions based on swaggerConfig
// const options = {
//     security: {
//         AccessTokenAuth: accessTokenAuth
//     }
// }
// const connectSwagger = connector(api, apiDefinition, options)
const connectSwagger = connector(api, apiDefinition)
// 使用接口！！！
connectSwagger(app)
// Print swagger router api summary
const apiSummary = summarise(apiDefinition)
console.log('apiSummary .... ', apiSummary)


// Catch 404 error
app.use((req, res, next) => {
    const err = new Error('Not Found...')
    res.status(404).json({
        message: err.message,
        error: err
    })
})

// Create HTTP server.
const server = http.createServer(app)

// Listen on provided port, on all network interfaces.
server.listen(port)
server.on('error', onError)
console.log('Ha... Mock server started on port ' + port + '!')

// Event listener for HTTP server "error" event.
function onError(error: any) {
    if (error.syscall !== 'listen') {
        throw error
    }
    const bind = typeof port === 'string' ? 'Pipe' + port : 'Port ' + port
    // handle specific listen errors with friendly messages
    switch (error.code) {
        case 'EACCES':
            console.error('Express ERROR (app) : %s requires elevated privileges', bind)
            // 强制进程退出
            process.exit(1)
        case 'EADDRINUSE':
            console.error('Express ERROR (app) : %s is already in use', bind)
            process.exit(1)
        default:
            throw error
    }
}